<?php
session_start();
if($_SESSION['is_login'] !== 'yes'){
    header("Location:/login.php");
    exit;
}
define('YANGJIFANG',true);
require_once "db.php";
$db = new Db();


$id = intval($_GET['id']);
if($id < 0){
    $id = 0;
}

$sql = 'select * from posts where id = '.$id;
$article = $db->getOne($sql);

$message = '';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $data = [];
    $data['title'] = isset($_POST['title'])?trim($_POST['title']):'';
    $data['content'] = isset($_POST['content'])?trim($_POST['content']):'';
    $data['update_time'] = time();

    $message = '操作失败';
    if($data['title'] && $data['content'] ){
        $affect = $db->update($data,$id,'posts');
        if($affect) $message = '操作成功';

        $sql = 'select * from posts where id = '.$id;
        $article = $db->getOne($sql);
    }
}
?>
<html>
<head>
    <style type="text/css">
        .title{
            padding:10px 0;
        }
        input{
            width:500px;
        }
        .content textarea{
            height:500px;
            width:500px;
        }
        .submit{
            padding:10px 0 10px 50px;
        }
    </style>
</head>
<body>
<div><?php echo $message; ?></div>
<div>
    <a href="/admin-list.php">列表</a>
    <a href="/logout.php">退出登录</a>
</div>
<form action="" method="post">
    <div class="title">
        标题：<input type="text" name="title" value="<?php echo $article['title'];?>" />
    </div>
    <div class="content">
        内容：<textarea name="content"><?php echo $article['content'] ?></textarea>
    </div>
    <input type="hidden" value="<?php echo $article['id'];?>" name="id" />
    <div class="submit">
        <button type="submit">提交</button>
    </div>
</form>
</body>
</html>